import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:zendesk_plugin/library.dart';

const String kZendeskIdentifierIos =
    'eyJzZXR0aW5nc191cmwiOiJodHRwczovL2ltc2N2LnplbmRlc2suY29tL21vYmlsZV9zZGtfYXBpL3NldHRpbmdzLzAxSlgxRlNXVDU0NFhYN1FZTVpLRFFWTVk5Lmpzb24ifQ==';

const String kZendeskIdentifierAndroid =
    'eyJzZXR0aW5nc191cmwiOiJodHRwczovL2ltc2N2LnplbmRlc2suY29tL21vYmlsZV9zZGtfYXBpL3NldHRpbmdzLzAxSlgyN0EzWUEwSEU2MEhKUzBWUDZXMTZaLmpzb24ifQ==';

void main() {
  runApp(const MyApp());
  configLoading();
}

void configLoading() {
  EasyLoading.instance
    ..displayDuration = const Duration(milliseconds: 2000)
    ..indicatorType = EasyLoadingIndicatorType.fadingCircle
    ..loadingStyle = EasyLoadingStyle.dark
    ..indicatorSize = 45.0
    ..radius = 10.0
    ..progressColor = Colors.yellow
    ..backgroundColor = Colors.green
    ..indicatorColor = Colors.yellow
    ..textColor = Colors.yellow
    ..maskColor = Colors.blue.withOpacity(0.5)
    ..userInteractions = true
    ..dismissOnTap = false;
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: EasyLoading.init(), // ✅ 关键初始化
      home: const HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Center(
              child: ElevatedButton(
                onPressed: () {
                  EasyLoading.showToast('Initializing Zendesk SDK...');
                  ZendeskPluginHelper.initZendeskSDK(
                          zendeskIdentifier: Platform.isIOS
                              ? kZendeskIdentifierIos
                              : kZendeskIdentifierAndroid)
                      .then((success) {
                    EasyLoading.dismiss();
                    Future.delayed(const Duration(milliseconds: 100)).then(
                        (_) => EasyLoading.showToast(success == true
                            ? 'Zendesk SDK initialized successfully'
                            : 'Failed to initialize Zendesk SDK'));
                  }).catchError((error) {
                    EasyLoading.showToast(
                        'Error initializing Zendesk SDK: $error');
                  }).whenComplete(() {
                    EasyLoading.dismiss();
                  });
                },
                child: const Text('Init Zendesk SDK'),
              ),
            ),
            Center(
              child: ElevatedButton(
                onPressed: () {
                  EasyLoading.showToast('Navigating to Conversation...');
                  ZendeskPluginHelper.pushToConversation()
                      .whenComplete(() => EasyLoading.dismiss());
                },
                child: const Text('Show Conversation'),
              ),
            ),
            Center(
              child: ElevatedButton(
                onPressed: () {
                  ZendeskPluginHelper.getUnreadCount().then((value) {
                    EasyLoading.showToast(
                      value != null
                          ? 'Unread messages count: $value'
                          : 'Failed to get unread messages count',
                    );
                  });
                },
                child: const Text('Get Unread Count'),
              ),
            ),
            Center(
              child: ElevatedButton(
                onPressed: () {
                  ZendeskPluginHelper.setLoggerEnabled(true).then((value) {
                    EasyLoading.showToast(
                      value == true
                          ? 'Logger enabled successfully'
                          : 'Failed to enable logger',
                    );
                  });
                },
                child: const Text('Enable Logger'),
              ),
            ),
            Center(
              child: ElevatedButton(
                onPressed: () {
                  ZendeskPluginHelper.setLoggerLevel(
                    level: ZendeskLoggerLevel.debug,
                  ).then((value) {
                    EasyLoading.showToast(
                      value == true
                          ? 'Logger level set to Debug successfully'
                          : 'Failed to set logger level',
                    );
                  });
                },
                child: const Text('Set Logger Level to Debug'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
